package cn.com.tarotframework.utils.response;

import java.util.Arrays;
import java.util.Optional;

/**
 * copyright (C), 2022, 塔罗牌基础架构
 * @program: tarot-utils
 * @description: 响应状态枚举类
 * @author: xincan
 * @date: 2022/05/20 16:44
 * @version: 0.0.1-SNAPSHOT
 */
public enum TarotCode {

    /**
     * 请求成功
     */
    REQUEST_SUCCESS(200, "操作成功"),
    REQUEST_ERROR(201, "操作失败"),
    REQUEST_SERVICE_ERROR(202, "服务请求频繁,请稍后重试"),

    BAD_REQUEST(400,"请求异常，数据格式不符"),
    UNAUTHORIZED(401,"请求异常，登录凭证过期"),
    FORBIDDEN(403,"请求异常，暂无权限访问"),
    NOT_FOUND(404, "请求异常，无法找到资源或请求地址错误"),
    METHOD_NOT_ALLOWED(405, "请求异常，当前请求方式与服务端接口接收方式不一致"),
    UNSUPPORTED_MEDIA_TYPE(415, "请求异常，当前媒体类型与服务端接口媒体类型不一致"),

    INTERNAL_SERVER_ERROR(500, "系统异常"),

    BUSINESS_EXCEPTION(3000, "业务异常"),

    INTERFACE_EXCEPTION(4000, "接口异常"),

    PARAM_EXCEPTION(5000, "参数异常"),

    RPC_ERROR(6000, "远程调用异常")
    ;

    /**
     * 状态码
      */
    private final int code;

    /**
     * 状态解释
     */
    private final String message;

    TarotCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     *  返回状态码的数值
     * @author xincan
     * @date 2022/1/8 19:45
     * @return Integer
     */
    public int code() {
        return this.code;
    }

    /**
     *  返回状态码的解释
     * @author xincan
     * @date 2022/1/8 19:45
     * @return java.lang.String 状态解释
     */
    public String message() {
        return this.message;
    }

    public static TarotCode getTarotCode(Integer code){
        return Arrays.stream(TarotCode.values())
                .filter(e -> e.code == code)
                .findFirst()
                .orElse(null);
    }

}
